using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace AdaptiveHelpDesk
{
    class UpdateTaskModel
    {
        XmlElement Steps;
        XmlElement SearchCustomer;

        XmlElement LogCall;
        XmlElement CallCat;
        XmlElement CallClass;

        XmlElement AssignCall;

        XmlElement ProvideSol;
        XmlElement SolClass;

        XmlElement Current;

        XmlElement currentUser; 

        XmlDocument doc;

        String tm = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Knowledge Base/Task Model/TaskModel.xml");  

        public UpdateTaskModel()
        {
            
            doc = new XmlDocument();
            doc.Load(tm);

            currentUser = (XmlElement)doc.DocumentElement.FirstChild;
            Steps = (XmlElement)doc.DocumentElement.FirstChild.NextSibling.NextSibling;
            Current = (XmlElement)doc.DocumentElement.LastChild;

            //Search Customer
            Step1SearchCustomer();

            //Log A Call
            Step2LogACall();
            CallCategorisation();
            CallClassification();

            //Assign Call
            Step3AssignCall();

            //Provide Solution
            Step4ProvideSolution();
            SolClassification();    
        
            //CurrentStep
        }

        private void UpdateXML()
        {
            doc.Save(tm);
        }

        //1 SEARCH CUSTOMER
        #region
        private void Step1SearchCustomer()
        {
            SearchCustomer = (XmlElement)Steps.GetElementsByTagName("SearchCustomer")[0];
        }

            public void UpdateSearchBy(string value)
            {
                SearchCustomer.ChildNodes[0].InnerText = value;
                UpdateXML();
            }

            public void UpdateSearchValue(string value)
            {
                SearchCustomer.ChildNodes[1].InnerText = value;
                UpdateXML();
            }

            public void UpdateSelectCustomer(string value)
            {
                SearchCustomer.ChildNodes[2].InnerText = value;
                UpdateXML();
            }
        #endregion


        //2 LOG CALL
        #region
        private void Step2LogACall()
        {
            LogCall = (XmlElement)Steps.GetElementsByTagName("LogCall")[0];
        }

            public void UpdateCallDescr(string value)
            {
                LogCall.ChildNodes[0].InnerText = value;
                UpdateXML();
            }

            private void CallCategorisation()
            {
                CallCat = (XmlElement)LogCall.GetElementsByTagName("CallCategorisation")[0];
            }

                public void UpdateServiceName(string value)
                {
                    CallCat.ChildNodes[0].InnerText = value;
                    UpdateXML();
                }

                public void UpdateCallType(string value)
                {
                    CallCat.ChildNodes[1].InnerText = value;
                    UpdateXML();
                }

                public void UpdateSubCallType(string value)
                {
                    CallCat.ChildNodes[2].InnerText = value;
                    UpdateXML();
                }

            private void CallClassification()
            {
                CallClass = (XmlElement)LogCall.GetElementsByTagName("Classification")[0];
            }

                public void UpdatePriority(string value)
                {
                    CallClass.ChildNodes[0].InnerText = value;
                    UpdateXML();
                }

                public void UpdateSource(string value)
                {
                    CallClass.ChildNodes[1].InnerText = value;
                    UpdateXML();
                }

        #endregion


        //3 ASSIGN A CALL
        #region
        private void Step3AssignCall()
        {
            //This is for assignee search, but we leave the name as 'AssigneeCall' - 'AssigneeSearch' is the firstChild of 'AssigneeCall'
            AssignCall = (XmlElement)Steps.GetElementsByTagName("AssigneeSearch")[0];
        }

            public void UpdateCampus(string value)
            {
                AssignCall.ChildNodes[0].InnerText = value;
                UpdateXML();
            }

            public void UpdateContact(string value)
            {
                AssignCall.ChildNodes[1].InnerText = value;
                UpdateXML();
            }
        #endregion


        //4 PROVIDE SOLUTION
        #region
        private void Step4ProvideSolution()
        {
            ProvideSol = (XmlElement)Steps.GetElementsByTagName("ProvideSolution")[0];
        }

            public void UpdateSolDescr(string value)
            {
                ProvideSol.ChildNodes[0].InnerText = value;
                UpdateXML();
            }

            private void SolClassification()
            {
                SolClass = (XmlElement)ProvideSol.GetElementsByTagName("SolutionClassification")[0];
            }

                public void UpdateCause(string value)
                {
                    SolClass.ChildNodes[0].InnerText = value;
                    UpdateXML();
                }

                public void UpdateHowResolved(string value)
                {
                    SolClass.ChildNodes[1].InnerText = value;
                    UpdateXML();
                }

        #endregion

        //CURRENT STEP
        public void UpdateCurrentStep(string value)
        {
            Current.InnerText = value;
            UpdateXML();
        }


        //set current user
        public String SetCurrentUser(String userid) //move to user model
        {
            try
            {
                 currentUser.InnerText = userid;
                 UpdateXML();
                 return "";
            }
            catch (Exception ex)
            {
                return ex.StackTrace;
            }
        }


    }
}
